package cn.kooki.app.duobao.ui.widget;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.ui.view.LoadingProgressView;

/**
 * Created by xiong on 2015/12/12.
 */
public class CustomRelativeLayout extends RelativeLayout implements View.OnClickListener {

    private TextView textView;
    private LoadingProgressView progressView;
    private boolean iswhitle = false;
    private View netWorkErrorWarpper;
    private View retry;

    private onRetryClickListener retryClickListener;




    public void setRetryClickListener(onRetryClickListener retryClickListener) {
        this.retryClickListener = retryClickListener;
    }


    @Override
    public void onClick(View v) {
        if (v == retry) {
            if (retryClickListener != null) {
                retryClickListener.onRetry();
            }
        }
    }

    public interface onRetryClickListener {
        void onRetry();
    }


    public CustomRelativeLayout(Context context) {
        this(context, null);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public boolean isShowNetError() {
        if (netWorkErrorWarpper != null) {
            return netWorkErrorWarpper.getVisibility() == VISIBLE;
        } else {
            return false;
        }
    }

    public void setIswhitle(boolean iswhitle) {
        this.iswhitle = iswhitle;
        progressView.setIswhitle(iswhitle);
    }




    private void init() {

        LayoutInflater.from(getContext()).inflate(R.layout.no_network, this, true);
        netWorkErrorWarpper = findViewById(R.id.network_error_warpper);
        retry = findViewById(R.id.retry);
        retry.setOnClickListener(this);
        netWorkErrorWarpper.setVisibility(GONE);
        textView = new TextView(getContext());
        textView.setTextColor(Color.parseColor("#ff4f4f4f"));
        LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(CENTER_IN_PARENT);

        textView.setLayoutParams(layoutParams);
        textView.setTextSize(14);
        textView.setVisibility(GONE);
        addView(textView);
        progressView = new LoadingProgressView(getContext());
        progressView.setLayoutParams(layoutParams);
        progressView.setVisibility(GONE);
        addView(progressView);

    }

    public void showNetError() {
        if (netWorkErrorWarpper != null) {
            bringChildToFront(netWorkErrorWarpper);
            netWorkErrorWarpper.setVisibility(VISIBLE);
        }
    }

    public void hideNetError() {
        if (netWorkErrorWarpper != null) {
            netWorkErrorWarpper.setVisibility(GONE);
        }
    }

    public void setNoText(String text) {
        if (textView != null) {
            textView.setText(text);
            textView.setVisibility(VISIBLE);
        }
    }

    public void hideNoText() {
        if (textView != null) {
            textView.setVisibility(GONE);
        }
    }

    public void showloading() {
        if (progressView != null) {
            bringChildToFront(progressView);
            progressView.setVisibility(VISIBLE);
            progressView.startAniation();
        }
    }

    public void hideloading() {
        if (progressView != null) {
            progressView.stopAniation();
            progressView.setVisibility(GONE);
        }
    }


    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }
}
